package com.sincereaddon.Service;

import com.alibaba.fastjson.JSON;
import com.sincereaddon.dao.IretailerDao;
import com.sincereaddon.dao.SincereaDao;
import com.sincereaddon.model.CameraMinute;
import com.sincereaddon.model.Device;
import com.sincereaddon.model.Passengerflow;
import com.sincereaddon.util.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by xjw on 2018/1/9.
 */
@Component
public class ScheduledService implements SchedulingConfigurer {

    private Logger log = LoggerFactory.getLogger(ScheduledService.class);

    @Autowired
    private IretailerDao iretailerDao;
    @Autowired
    private SincereaDao sincereaDao;
    @Value("${sync.cron}")
    private String cron;
    @Value("${sql.limit}")
    private Integer limit;

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addTriggerTask(new Runnable() {
            @Override
            public void run() {
                //定时任务逻辑
                log.info(TimeUtil.getDateTime(new Date()));
                List<Device> devices = iretailerDao.getAllDevice(0);
//                log.info("devicesSize: "+devices.size()+" "+JSON.toJSONString(devices));
                for (Device d : devices) {
                    if (d.getZoneId() != null) {
                        List<Passengerflow> insertPassenger = new ArrayList<>();

                        List<CameraMinute> cameraMinutes = sincereaDao.getTblCountInfoCameraMinute(d.getDeviceNameId(), d.getUpdateTime(), limit);
                        if (!cameraMinutes.isEmpty()) {
                            for (CameraMinute ca : cameraMinutes) {
                                Passengerflow p = new Passengerflow();
                                String dateTime = ca.getCounttime();
                                String date = dateTime.substring(0, 10);
                                String dateHour = dateTime.substring(11, 13);
                                String dateMin = dateTime.substring(14, 16);

                                p.setZoneId(d.getZoneId());
                                p.setDateTime(dateTime);
                                p.setDate(date);
                                p.setDateHour(Integer.parseInt(dateHour));
                                p.setDateMin(Integer.parseInt(dateMin));
                                p.setCountEnter(ca.getInnum());
                                p.setCountExit(ca.getOutnum());

                                insertPassenger.add(p);
                            }

                        }
                        if (!insertPassenger.isEmpty()) {
                            int insertCount = iretailerDao.batchReplacePassengerflow(insertPassenger);
                            int size = insertPassenger.size()-1;
                            String updateTime = insertPassenger.get(size).getDateTime();
                            int updateCount = iretailerDao.updateDevice(d.getDeviceNameId(),updateTime);
                            log.info("size: "+insertPassenger.size()+" deviceNameId: "+d.getDeviceNameId()+" updateTime: "+updateTime);
                        }

                    }
                }


            }
        }, new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                //顶死任务触发，可修改定时任务的执行周期
                CronTrigger trigger = new CronTrigger(cron);
                Date nextExecDate = trigger.nextExecutionTime(triggerContext);
                return nextExecDate;
            }
        });
    }
}
